﻿using System;
using System.Collections.Generic;
using DBUtility;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace IYAKASAPortal.Statistics
{
    public partial class TJ_DPJYY : System.Web.UI.Page
    {
        private static string s_strStart;
        private static string s_strEnd;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string d1 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询开始时间
                string d2 = string.Format("{0}", DateTime.Now.ToString("yyyy-MM-dd"));   //默认查询截止时间

                txtStart.Text = d1;
                txtEnd.Text = d2;
                btnSearch_Click(null, null);
            }

            string checkType = string.Empty;
            if (Request["CheckType"] != null && !string.IsNullOrEmpty(Request["CheckType"]))
            {
                checkType = Request["CheckType"].ToString().Substring(0, 1);
            }

            if (checkType == "Z")
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--引车员分类合格率汇总表(综检)";
                lblTistle.Text = "检测线分类合格率汇总表(综检 - 底盘检验员)";
            }
            else if (checkType == "P")
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--引车员分类合格率汇总表(排放)";
                lblTistle.Text = "检测线分类合格率汇总表(排放 - 底盘检验员)";
            }
            else
            {
                TableTitle.InnerText = "成都弥荣科技发展有限公司--引车员分类合格率汇总表(安检)";
                lblTistle.Text = "检测线分类合格率汇总表(安检 - 底盘检验员)";
            }
            lblJGMC.Text = string.Format("机动车安全技术检验机构名称:{0}",StaInfoTools.GetStationName());
        }


        private DataTable ReSetDataTable(DataTable dt)
        {

            int iZJSL = 0, iZJHGS = 0, iCJSL = 0, iCJHGS = 0, iFJSL = 0, iFJHGS = 0;

            foreach (DataRow dr in dt.Rows)
            {
                iZJSL += Convert.ToInt32(dr["ZJSL"]);
                iCJSL += Convert.ToInt32(dr["CJSL"]);
                iFJSL += Convert.ToInt32(dr["FJSL"]);
            }


            //合计行
            DataRow tDr = dt.NewRow();
            tDr["DPJYY"] = "合计";
            tDr["ZJSL"] = iZJSL;
            tDr["CJSL"] = iCJSL;
            tDr["FJSL"] = iFJSL;
            dt.Rows.Add(tDr);
            return dt;
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string startDate = txtStart.Text;
            string endDate = txtEnd.Text;

            //当条件为空的查询范围 加减100年
            DateTime dFuture = DateTime.Now.AddYears(100);
            DateTime dPast = DateTime.Now.AddYears(-100);

            //获取表中的最大最小日期
            string maxDateSql = "SELECT TOP 1 JCRQ FROM RESULT_VEHICLE_INFO WHERE JCRQ IS NOT NULL  ORDER BY JCRQ DESC";
            string minDateSql = "SELECT TOP 1 JCRQ FROM RESULT_VEHICLE_INFO WHERE JCRQ IS NOT NULL  ORDER BY JCRQ ASC";
            string maxDate = DbHelper.GetValue(maxDateSql);
            string minDate = DbHelper.GetValue(minDateSql);

            if (txtStart.Text == string.Empty && txtEnd.Text == string.Empty)
            {
                endDate = dFuture.ToString("yyyy-MM-dd");
                startDate = dPast.ToString("yyyy-MM-dd");

                lblDate.Text = string.Format("{0} - {1}", minDate, maxDate);
            }
            else if (txtStart.Text == string.Empty)
            {
                startDate = dPast.ToString("yyyy-MM-dd");
                lblDate.Text = string.Format("{0} - {1}", minDate, endDate);
            }
            else if (txtEnd.Text == string.Empty)
            {
                endDate = dFuture.ToString("yyyy-MM-dd");
                lblDate.Text = string.Format("{0} - {1}", minDate, maxDate);
            }
            else
            {
                lblDate.Text = string.Format("{0} - {1}", startDate, endDate);
            }
            DBUtility.DbStoredProcedure _sqlSpExecute = new DbStoredProcedure();
            string checkType = string.Empty;
            if (Request["CheckType"] != null && !string.IsNullOrEmpty(Request["CheckType"]))
            {
                checkType = Request["CheckType"].ToString().Substring(0, 1);
            }

            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@StartTime",startDate),
                new SqlParameter("@EndTime",endDate),
                new SqlParameter("@checkType",checkType)
              };
            DataTable dt_TempSet = new DataTable();
            _sqlSpExecute.GetProcDataTable(ref dt_TempSet, "proc_RESULT_VEHICLE_INFO_GetDPJYYData", param);

            this.rptList.DataSource = ReSetDataTable(dt_TempSet);
            this.rptList.DataBind();
            dt_TempSet.Dispose();
        }

        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            this.txtStart.Text = string.Empty;
            this.txtEnd.Text = string.Empty;
            btnSearch_Click(sender, e);
        }
    }
}